from django.db import models
import django.utils.timezone as timezone
from django.db.models.functions import ExtractDay


class GoodsCategory(models.Model):
    name = models.CharField(unique=True, verbose_name="商品种类", blank=False, max_length=10)

    class Meta:
        verbose_name = "商品种类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(models.Model):
    name = models.CharField(verbose_name="商品名称", blank=False, max_length=20)
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, verbose_name="商品种类")
    cost = models.FloatField(verbose_name="成本", blank=False)
    selling_price = models.FloatField(verbose_name="售价", blank=False)
    quantity_in_store = models.IntegerField(verbose_name="库存数量", default=0)
    specification = models.CharField(verbose_name="规格", blank=False, max_length=8)
    unit = models.CharField(verbose_name="单位", blank=False, max_length=4)
    add_time = models.DateTimeField(verbose_name="添加日期", auto_now_add=True)

    class Meta:
        verbose_name = "商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Customer(models.Model):
    organization = models.CharField(verbose_name="单位名称", unique=True, max_length=70)
    address = models.CharField(verbose_name="单位地址", unique=True, max_length=70)
    IS_CUSTOMER = ((0, "客户"), (1, "商家"))
    is_customer = models.BooleanField(verbose_name="单位类型", choices=IS_CUSTOMER, default=0)
    add_time = models.DateTimeField(verbose_name="入单日期", auto_now_add=True)

    class Meta:
        verbose_name = "单位机构"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.organization

#
# class Member(models.Model):
#     organization = models.ForeignKey(Customer, on_delete=models.CASCADE, verbose_name="组织")
#     name = models.CharField(verbose_name="成员姓名", max_length=5)
#     phone = models.CharField(verbose_name="电话/手机", max_length=13)
#
#     class Meta:
#         verbose_name = "机构成员"
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return "%s: %s %s" % (self.organization.organization, self.name, self.phone)


class StorageRecord(models.Model):
    number = models.CharField(verbose_name="订单编号（格式：年-月-日-时-分-六位随机数字）", unique=True, max_length=25)
    organization = models.ForeignKey(Customer, on_delete=models.CASCADE, verbose_name="商家单位", blank=False)
    total_pries = models.FloatField(verbose_name="共计", max_length=5)
    # handled_by = models.ForeignKey(Member, on_delete=models.CASCADE, verbose_name="客户成员")
    create_time = models.DateTimeField(verbose_name="时间", default=timezone.now)
    whether_to_make_out_an_invoice = models.BooleanField(verbose_name="是否开票", default=False)

    class Meta:
        verbose_name = "出库记录"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 共计：%.2f" % (self.organization.organization, self.total_pries)


class StorageRecordOfGoods(models.Model):
    storage_record = models.ForeignKey(StorageRecord, on_delete=models.CASCADE, verbose_name="哪条出库记录")
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品名")
    amount = models.IntegerField(verbose_name="数量", default=1)
    pries = models.FloatField(verbose_name="单价", blank=False, max_length=5)
    cost = models.FloatField(verbose_name="成本", blank=False, max_length=5)
    total_pries = models.FloatField(verbose_name="金额", blank=False, max_length=5)

    class Meta:
        verbose_name = "出库票上的商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s %s * %d 共计：%.2f" % (self.storage_record.organization, self.goods.name, self.amount, self.total_pries)


class TicketSetting(models.Model):
    head = models.CharField(verbose_name="单子名称", max_length=30)
    tip = models.CharField(verbose_name="温馨提示", max_length=60)
    phone = models.IntegerField(verbose_name="手机")
    number = models.IntegerField(verbose_name="电话")
    is_danger = models.IntegerField(verbose_name="少于多少报警")
    address = models.CharField(verbose_name="地址", max_length=60)
    make_by = models.CharField(verbose_name="制单人", max_length=5)
    send_by = models.CharField(verbose_name="送货人", max_length=5)

    def __str__(self):
        return self.head

    class Meta:
        verbose_name = "系统基本设置"
        verbose_name_plural = verbose_name

